package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/Item")
@SessionAttributes({"productList", "categoryList","itemList","updateCategoryId"})
public class ItemController {

    @Autowired
    ItemService itemService;

    //进入商品类型管理界面
    @GetMapping("/viewItem_type")
    public String viewItemType(Model model) {
        List<Product> productList = itemService.findAllProduct();
        List<Category> categoryList = itemService.findAllCategory();
        model.addAttribute("productList", productList);
        model.addAttribute("categoryList", categoryList);
        return "Item/item_type";
    }

    //进入商品信息管理界面
    @GetMapping("/viewItem_info")
    public String viewItemInfo(Model model) {
        List<Item> list = itemService.findAllItem();
        model.addAttribute("itemList",list);
        return "Item/item_info";
    }

    //根据选择的商品类型搜索商品
    @PostMapping("/searchByType")
    public String searchByType(String type_select, String content, Model model) {
        if (type_select.equals("product")) {
            List<Product> productList = itemService.findProductByName(content);
            model.addAttribute("productList", productList);
        } else {
            List<Category> categoryList = itemService.findCategoryByName(content);
            model.addAttribute("categoryList", categoryList);
        }
        return "Item/item_type::table_refresh";
    }

    //进入添加Category的页面
    @GetMapping("/viewAddCategory")
    public String viewAddCategory() {
        return "Item/item_addcategory";
    }

    //进入添加Product的页面
    @GetMapping("/viewAddProduct")
    public String viewAddProduct() {
        return "Item/item_addproduct";
    }

    //在添加大类页面中，点击提交后，将添加的内容插入数据库，然后返回商品类型页面做一个展示
    @PostMapping("/addCategory")
    public String addCategory(String categoryId, String name, String description, Model model) {
        itemService.insertCategory(categoryId, name, description);
        List<Category> categoryList = itemService.findAllCategory();
        model.addAttribute("categoryList", categoryList);
        return "Item/item_type";
    }

    //在添加小类页面中，点击提交后，将添加的内容插入数据库，然后返回商品类型页面做一个展示
    @PostMapping("/addProduct")
    public String addProduct(String productId, String category, String name, String description, Model model) {
        itemService.insertProduct(productId,category, name, description);
        List<Product> productList = itemService.findAllProduct();
        model.addAttribute("productList", productList);
        return "Item/item_type";
    }

    //根据itemId或者productId查询相应的item
    @PostMapping("/searchByInfo")
    public String searchByItemIdOrProductId(String itemId,String productId,Model model){
        List<Item> list;
        if(productId.length()==0){
            //执行根据itemId查询内容
            list = itemService.findItemByItemId(itemId);
        }else if(itemId.length()==0){
            //执行根据productId查询内容
            list = itemService.findItemByProductId(productId);
        }else{
            //同时根据productId和itemId查询内容
            list = itemService.findItemByItemIdAndProductId(itemId,productId);
        }
        model.addAttribute("itemList",list);
        return "Item/item_info::table_refresh";
    }

    //进入商品添加页面
    @GetMapping("/viewAddItem")
    public String viewAddItem(){return "Item/item_mdf";}

    //添加商品，添加完之后跳转至展示页面
    @PostMapping("/addItem")
    public String addItem(Item item, Model model){
        itemService.insertItem(item);
        List<Item> list = itemService.findAllItem();
        model.addAttribute("itemList",list);
        return "Item/item_info";
    }

    //删除category
    @GetMapping("/deleteCategory")
    public String deleteCategory(String categoryId, Model model){
        itemService.deleteCategoryByCategoryId(categoryId);
        List<Category> categoryList = itemService.findAllCategory();
        model.addAttribute("categoryList", categoryList);
        return "Item/item_type";
    }

    //删除product
    @GetMapping("/deleteProduct")
    public String deleteProduct(String productId,Model model){
        itemService.deleteProductByProductId(productId);
        List<Product> productList = itemService.findAllProduct();
        model.addAttribute("productList", productList);
        return "Item/item_type";
    }

    //删除test
    @GetMapping("/deleteItem")
    public String deleteItem(String itemId, Model model){
        itemService.deleteItemByItemId(itemId);
        List<Item> list = itemService.findAllItem();
        model.addAttribute("itemList",list);
        return "Item/item_info";
    }

    //进入修改category界面
    @GetMapping("/viewUpdateCategory")
    public String viewUpdateCategory(String categoryId, String name, String description,Model model){
        model.addAttribute("categoryId",categoryId);
        //用于记录拿个记录需要更新
        model.addAttribute("updateCategoryId",categoryId);
        model.addAttribute("name",name);
        model.addAttribute("description",description);
        return "Item/item_editcategory";
    }


    //修改category
//    //这个失败了
//    @PostMapping("/updateCategory")
//    public String updateCategory(@SessionAttribute("updateCategoryId") String updateCategoryId, String categoryId, String name, String description, Model model){
//        //先将category修改
////        itemService.updateCategoryByCategoryId(updateCategoryId,categoryId,name,description);
//        //再删除session中的updateCategoryId这个属性，返回展示界面
//        List<Category> categoryList = itemService.findAllCategory();
//        model.addAttribute("categoryList", categoryList);
//        return "Item/item_type";
//    }
    @PostMapping("/updateCategory")
    public String updateCategory(String categoryId, String name, String description, Model model){
        //先将category修改
        itemService.updateCategoryByCategoryId(categoryId,name,description);
        List<Category> categoryList = itemService.findAllCategory();
        model.addAttribute("categoryList", categoryList);
        return "Item/item_type";
    }

    //进入修改product的页面
    @GetMapping("/viewUpdateProduct")
    public String viewUpdateProduct(Model model,String categoryId,String productId,String name,String description){
        model.addAttribute("productId",productId);
        model.addAttribute("name", name);
        model.addAttribute("description",description);
        model.addAttribute("categoryId",categoryId);
        return "Item/item_editproduct";
    }
    //修改product
    @PostMapping("/updateProduct")
    public String updateProduct(String productId,String name, String description, Model model){
        itemService.updateProductByProductId(productId,name,description);
        List<Product> productList = itemService.findAllProduct();
        model.addAttribute("productList", productList);
        return "Item/item_type";
    }
    //进入修改item页面
    @GetMapping("/viewUpdateItem")
    public String viewUpdateItem(String itemId,String productId,String listPrice,String unitCost,String supplierId,String status,String attr,String quantity,Model model){
        BigDecimal bigDecimal1 = new BigDecimal(listPrice);
        BigDecimal bigDecimal2 = new BigDecimal(unitCost);
        Item item = new Item(itemId,productId,bigDecimal1,bigDecimal2,Integer.parseInt(supplierId),status,attr,Integer.parseInt(quantity));
        model.addAttribute("item",item);
        return "Item/item_edit";
    }
    //修改item
    @PostMapping("/updateItem")
    public String updateItem(Item item, Model model){
        itemService.updateItemByItemId(item);
        List<Item> list = itemService.findAllItem();
        model.addAttribute("itemList",list);
        return "Item/item_info";
    }
}
